package com.atguigu;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadDemon2 {

	public static void main(String[] args) {

		final Relay re = new Relay();
		final Lock l = new ReentrantLock();
		final Condition con1 = l.newCondition();
		final Condition con2 = l.newCondition();
		final Condition con3 = l.newCondition();
		new Thread(new Runnable() {

			@Override
			public void run() {
				l.lock();
				try {
					for (int i = 0; i < 10; i++) {
						
						re.printA();
						con1.await();
						con2.signal();
					}
				} catch (Exception e) {

				} finally {
					l.unlock();
				}
			}
		}, "AA").start();

		new Thread(new Runnable() {

			@Override
			public void run() {
				l.lock();
				try {
					for (int i = 0; i < 10; i++) {
						
						re.printB();
						con2.await();
						con3.signal();
					}
				} catch (Exception e) {

				} finally {
					l.unlock();
				}
			}
		}, "BB").start();

		new Thread(new Runnable() {

			@Override
			public void run() {
				l.lock();
				try {
					for (int i = 0; i < 10; i++) {
						
						re.printC();
						
						con1.signal();
						con3.await();
					}
				} catch (Exception e) {

				} finally {
					l.unlock();
				}
			}
		}, "CC").start();

	}
}

class Relay {

	public void printA() {
		System.out.println(Thread.currentThread().getName() + "打印的~AAAAAAAAAA");
	}

	public void printB() {
		System.out.println(Thread.currentThread().getName() + "打印的~BBBBBBBBBB");
	}

	public void printC() {
		System.out.println(Thread.currentThread().getName() + "打印的~CCCCCCCCCC");
	}

}
